package gfs;

import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Mark
 */
public class StrategoPlayer extends Object
    {
    
    private ArrayList<StrategoSoldier> pieces;
    private boolean host;
           
    public StrategoPlayer(boolean local, Board bd, boolean isHost)
        {
        this(StrategoSoldier.getSoldierCompleteSet(local, bd));
        host = isHost;
        }
    
    public StrategoPlayer(ArrayList<StrategoSoldier> myPieces)
        {
        super();
        pieces = myPieces;
        }
    
    public ArrayList<StrategoSoldier> getSoldiers()
        {
        return pieces;
        }

    public boolean isHost() 
        {
        return host;
        }
    
    /**
     * Auxilliary method to set a soldier 
     * 
     * @param sold 
     *      The soldier to be set into the 
     * @return
     *      The soldier you have set into this player's soldier list
     */
    public StrategoSoldier setSoldier(StrategoSoldier sold)
        {
        pieces.add(sold);
        return sold;
        }
    
    /**
     * Removes a soldier from this player's pieces
     * 
     * @param sold
     *      The soldier to remove
     * @return
     *      The soldier that was removed
     */
    public StrategoSoldier removeSoldier(StrategoSoldier sold)
        {
        StrategoSoldier pc = null;
        for(int x = 0; x < pieces.size(); x++)
            if(pieces.get(x) == sold)
                pc = pieces.remove(x);
        return pc;
        }
    
    public boolean isMine(StrategoSoldier piece) throws NullPointerException
        {
        if (piece == null)
            {
            System.out.println("I'm null");
            //throw new NullPointerException();
            }
        for (StrategoSoldier ss: pieces)
            if (ss == piece)
                return true;
        return false;
        }
    
    /**
     * Draws all the soldiers of this <code>StrategoPlayer</code>     
     */
    public void displayAll()
        {
        for (StrategoSoldier ss: pieces)
            ss.draw(true);
        //return true;
        }

    /**
     * For set up only checks if the location which you are placing the soldier 
     * is empty
     * @param cd 
     *      The <code>Coordinate</code> to test if it is empty
     * @return 
     *      True if empty else false
     */
    public boolean isEmpty(Coordinate cd)
        {
        for (StrategoSoldier ss: pieces)
            if (ss.getCoordinate() != null && ss.getCoordinate().equals(cd))
                return false;
        return true;
        }
    
    /**
     * Sorts your pieces by hashcode in either ascending or descending order
     * 
     * @param order
     *      If true sorts in ascending else in descending
     */
    public void insertSortSelection(boolean order)
    	{
        StrategoSoldier hold = null;
    	int temp = 0, j = 0;
    	if(order)
    		{
    		for(int i = 1; i < pieces.size(); i++)
    			{
                        hold = pieces.get(i);
    			temp = pieces.get(i).getCoordinate().hashCode();
    			j = i - 1;
    			while(j >= 0 && pieces.get(j).getCoordinate().hashCode()
                                > temp)
    				pieces.set(j + 1, pieces.get(j--));
    			pieces.set(j + 1, hold);
    			}
    		}
    	if(!order)
    		{
    		for(int i = 1; i < pieces.size(); i++)
    			{
                        hold = pieces.get(i);
    			temp = pieces.get(i).getCoordinate().hashCode();
    			j = i - 1;
    			while(j >= 0 && pieces.get(j).getCoordinate().hashCode()
                                < temp)
    				pieces.set(j + 1, pieces.get(j--));
    			pieces.set(j + 1, hold);
    			}
    		}
    	}
    
    }

